package com.xiaodeng.xdblogspringboot.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xiaodeng.xdblogspringboot.exception.UserException;
import com.xiaodeng.xdblogspringboot.mapper.AdminMapper;
import com.xiaodeng.xdblogspringboot.mapper.UserMapper;
import com.xiaodeng.xdblogspringboot.pojo.Admin;
import com.xiaodeng.xdblogspringboot.pojo.Article;
import com.xiaodeng.xdblogspringboot.pojo.PageBean;
import com.xiaodeng.xdblogspringboot.pojo.User;
import com.xiaodeng.xdblogspringboot.service.AdminService;
import com.xiaodeng.xdblogspringboot.utils.JwtUtil;
import com.xiaodeng.xdblogspringboot.utils.Md5Util;
import com.xiaodeng.xdblogspringboot.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private UserMapper userMapper;

    // TODO 管理员登录
    @Override
    public String login(String username, String password) {
        //用户名查询用户
        Admin loginuser = this.findByUserName(username);
        if(loginuser == null){
            throw new UserException(401,"用户不存在");
        }
        if (!loginuser.getPassword().equals(Md5Util.getMD5String(password))){
            throw new UserException(402,"账号或者密码错误");
        }
        Map<String,Object> claims = new HashMap<>();
        claims.put("id",loginuser.getId());
        claims.put("username",loginuser.getUsername());
        claims.put("user_pic",loginuser.getUserPic());
        claims.put("role",1);
        return JwtUtil.genToken(claims);
    }
    // TODO 管理员注册
    @Override
    public void register(String username, String password, String email) {
        //用户名查询用户
        Admin u = this.findByUserName(username);
        if(u != null){
            throw new UserException(401,"该用户已存在");
        }
        //加密处理
        String md5String = Md5Util.getMD5String(password);
        //注册
        adminMapper.add(username,md5String,email);
    }



    @Override
    public void delete(Integer id) {
        adminMapper.delete(id);
    }
    //管理更新个人信息
    @Override
    public void update(Admin admin) {
        adminMapper.update(admin);
    }
    //更新用户头像
    @Override
    public void updateAvatar(String avatarUrl) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        adminMapper.updateAvatar(avatarUrl,id);
    }

    //用户修改密码
    @Override
    public void updatePwd(String newPwd, Admin admin) {
        //根据用户名拿到原密码跟新密码对比
        Map<String,Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        Integer id =(Integer) map.get("id");
        Admin loginUser = this.findByUserName(username);
        if (StrUtil.isBlank(admin.getPassword()) ){
            throw new UserException(401,"原密码为空");
        }
        if (StrUtil.isBlank(loginUser.getPassword())){
            throw new UserException(401,"新密码为空");
        }
        if (!loginUser.getPassword().equals(Md5Util.getMD5String(admin.getPassword()))){
            throw new UserException(402,"请输入正确的原密码");
        }
        adminMapper.updatePwd(Md5Util.getMD5String(newPwd), id);
    }
    //查询用户信息
    @Override
    public Admin findByUserName(String username) {
        return adminMapper.findByUserName(username);
    }

    // TODO  **获取用户列表**
    @Override
    public PageBean<User> userList(Integer pageNum, Integer pageSize, String username, String email) {
        //创建pageBean对象
        PageBean<User> pb = new PageBean<>();
        //开启分页查询
        PageHelper.startPage(pageNum,pageSize);
        //调用mapper
        List<User> as = adminMapper.userList(username,email);
        Page<User> p =(Page<User>) as;
        //把数据填充到pageBean
        //获取总条数
        pb.setTotal(p.getTotal());
        //获取当前页的数据
        pb.setItems(p.getResult());
        return pb;
    }

    // TODO **创建用户**
    @Override
    public void addUser(User user) {
        String password = "123456";
        //加密处理
        String md5String = Md5Util.getMD5String(password);
        user.setPassword(md5String);
        user.setNickname("user_"+user.getUsername());
        adminMapper.addUser(user);
    }
    // TODO **更新用户信息**
    @Override
    public User updateUser(User user) {
        adminMapper.updateUser(user);
        return user;
    }
    //重置用户密码
    @Override
    public void resetPwd(String newPwd, User user) {
        String username = user.getUsername();
        User u = userMapper.findByUserName(username);
        if (u == null) {
            throw new UserException(401,"用户不存在");
        }
        u.setPassword(newPwd);
        Integer id = u.getId();
        adminMapper.resetPwd(Md5Util.getMD5String(newPwd),id);

    }




}
